package com.cjd.common.utils;

import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.util.Map;
import java.util.TreeMap;

/**
 * @Author chenjidong
 * @email 374122600@qq.com
 * created 2019/10/19
 * description
 */
public class MD5Util {

    public static String encrypt2md5(String str) throws Exception {
        String hexStr = "";

        MessageDigest digest = MessageDigest.getInstance("MD5");
        byte[] bytes = digest.digest(str.getBytes(StandardCharsets.UTF_8));

        StringBuilder stringBuilder = new StringBuilder();
        for (byte b : bytes) {
            if ((b & 0xff) < 0x10) {
                stringBuilder.append("0");
            }
            stringBuilder.append(Integer.toHexString((b & 0xff)));
        }
        hexStr = stringBuilder.toString();

        return hexStr;
    }

    public static void main(String[] args) {
        String value ="tel=15580000000&time=1571192411&key=666YOUZHENGMy2ETEqjm¡±";
        TreeMap<String,String> map = new TreeMap<>();
        map.put("time","1571192411");
        map.put("tel","15580000000");
        try {
            StringBuilder builder = new StringBuilder();
            for (String key : map.keySet()){
                builder.append(key);
                builder.append("=");
                builder.append(map.get(key));
                builder.append("&");
            }
            builder.append("key=666YOUZHENGMy2ETEqjm¡±");
            String value1 =  encrypt2md5(builder.toString());
            System.out.println(value1);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
